﻿
Partial Class ScrumMaster_SprintPlanning
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim userID As String

        If Not Page.IsPostBack Then
            calStartDate.SelectedDate = Today
            calEndDate.SelectedDate = calStartDate.TodaysDate.AddDays(28)
            hSumOfFeatHours.Value = 0
            userID = Membership.GetUser.ProviderUserKey.ToString
            lSumOfFeatHours.Text = 0
			FillSupportingProjectData(userID)

        End If

        If calEndDate.SelectedDate.Month <> calStartDate.SelectedDate.Month Then
            calEndDate.VisibleDate = calEndDate.SelectedDate
        End If

    End Sub

    Private Sub FillSupportingProjectData(ByVal userID As String)
        Dim _bl As New scrumrBL

        _bl.FillProjectListForSMUserID(ddlProjects, userID)

        _bl = Nothing
	End Sub

	Private Sub FillPriorities(ByVal ddlPriorities As DropDownList)
		Dim _bl As New scrumrBL

		_bl.FillPrioritiesList(ddlPriorities)

		_bl = Nothing
	End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calStartDate.SelectionChanged
        calEndDate.SelectedDate = calStartDate.SelectedDate.AddDays(28)

        If calEndDate.SelectedDate.Month <> calStartDate.SelectedDate.Month Then
            calEndDate.VisibleDate = calEndDate.SelectedDate
        End If

        Dim _count As Integer
        _count = 0
        For Each _item As ListItem In lbDevTeam.Items
            If _item.Selected = "True" Then
                _count = _count + 1
            End If
        Next

        Dim _sp As New sprint

        _sp.FillDevHoursTotal(_count, calStartDate.SelectedDate, calEndDate.SelectedDate)
        _sp.FillDevTeamBullet(blDevTeam, lbDevTeam)
        hSumOfDevHours.Value = _sp.SprintTotalDevHours
        lSumOfDevHours.Text = _sp.SprintTotalDevHours
        lRemainDevHours.Text = hSumOfDevHours.Value - hSumOfFeatHours.Value
        lSummaryDevHoursValue.Text = _sp.SprintTotalDevHours


    End Sub

    Protected Sub ddlProjects_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProjects.SelectedIndexChanged

        Dim _sp As New sprint
        Dim userID As String
        userID = Membership.GetUser.ProviderUserKey.ToString

        FillSupportingFeaturesData(ddlProjects.SelectedValue)
        _sp.FillProjectPOandSM(ddlProjects.SelectedValue)
        FillSupportingDevTeamData()
        lProjectOwnerName.Text = _sp.ProjectOwner
        lScrumMasterName.Text = _sp.ScrumMaster
        lSummaryProjectValue.Text = ddlProjects.SelectedItem.Text
        lSummaryProjectOwnerValue.Text = _sp.ProjectOwner
        lSummaryScrumMasterValue.Text = _sp.ScrumMaster

    End Sub

    Private Sub FillSupportingFeaturesData(ByVal iprojectID As Integer)
        Dim _bl As New scrumrBL

        _bl.FillFeaturesListBox(lbFeatures, iprojectID)

        _bl = Nothing
    End Sub

    Private Sub FillSupportingDevTeamData()
        Dim _bl As New scrumrBL

        _bl.FillDevTeamListBox(lbDevTeam)

        _bl = Nothing
    End Sub

    Protected Sub bSprintHeaderFinish_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bSprintHeaderFinish.Click
        lSummaryEndValue.Text = calEndDate.SelectedDate
        lSummaryStartValue.Text = calStartDate.SelectedDate
        pSprintDetail.Visible = "true"
        pSprintHeaderSummary.Visible = "true"
        pSprintHeader.Visible = "false"
        pSubtasks.Visible = "true"
    End Sub

    Protected Sub bNewSprint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bNewSprint.Click
        Response.Redirect("SprintPlanning.aspx")
    End Sub

    Protected Sub bSprintDetailFinish_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bSprintDetailFinish.Click
        Dim _sp As New sprint

        lOutcome.Visible = True

        Try
            bNewSprint.Enabled = "True"
            pSprintDetail.Visible = "False"
            pSprintDetailSummary.Visible = "True"
            lAddedFeatures.Visible = "True"
            pSubtasks.Visible = "False"
            pSprintDetailSummary.Visible = "True"
            pTaskAddEdit.Visible = "false"
            _sp.AddFeaturesToBullet(lbFeaturesInSprint, blFeaturesInSprint)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub lbFeatures_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbFeatures.SelectedIndexChanged
        Dim _bl As New scrumrBL
        Dim _sp As New sprint

        _bl.FillSubtasksforFeature(gvSubtasks, lbFeatures.SelectedValue)
        _sp.FillFeatureHoursSum(lbFeatures.SelectedValue)
        _sp.FillFeatureSubtaskCount(lbFeatures.SelectedValue)
        lFeatEstHours.Text = _sp.FeatureHours
		lSubtaskCount.Text = _sp.FeatureTaskCount

		_bl = Nothing
		_sp = Nothing

	End Sub

    Protected Sub bAddToSprint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bAddToSprint.Click
        AddFeatureToSprint(lbFeatures.SelectedItem.Text, lbFeatures.SelectedValue)
    End Sub

    Private Sub AddFeatureToSprint(ByVal selectedItem As String, ByVal selectedValue As Integer)
        Dim _sp As New sprint
        Dim _bl As New scrumrBL
        hSumOfDevHours.Value = lSumOfDevHours.Text
        lbFeaturesInSprint.Items.Insert(0, New ListItem(selectedItem, selectedValue))
        _sp.AddEstHoursForSprint(lbFeatures.SelectedValue, hSumOfFeatHours.Value)
        lbFeatures.Items.Remove(lbFeatures.SelectedItem)
        hSumOfFeatHours.Value = _sp.PostSumOfFeatureHours
        lSumOfFeatHours.Text = hSumOfFeatHours.Value
        lRemainDevHours.Text = hSumOfDevHours.Value - hSumOfFeatHours.Value
        If lbFeatures.Items.Count() > 0 Then
            pSubtasks.Visible = "true"
            lbFeatures.SelectedIndex = 0
            _bl.FillSubtasksforFeature(gvSubtasks, lbFeatures.SelectedValue)
            _sp.FillFeatureHoursSum(lbFeatures.SelectedValue)
            _sp.FillFeatureSubtaskCount(lbFeatures.SelectedValue)
            lFeatEstHours.Text = _sp.FeatureHours
            lSubtaskCount.Text = _sp.FeatureTaskCount
        Else
            pSubtasks.Visible = "false"
            pTaskAddEdit.Visible = "false"
        End If
        If lbFeaturesInSprint.Items.Count() > 0 Then
            lbFeaturesInSprint.SelectedIndex = 0
        End If

    End Sub

    Protected Sub bRemoveFromSprint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bRemoveFromSprint.Click
        RemoveFeatureFromSprint(lbFeaturesInSprint.SelectedItem.Text, lbFeaturesInSprint.SelectedValue)
    End Sub

    Private Sub RemoveFeatureFromSprint(ByVal selectedItem As String, ByVal selectedValue As Integer)
        Dim _sp As New sprint
        Dim _bl As New scrumrBL
        lbFeatures.Items.Insert(0, New ListItem(selectedItem, selectedValue))
        _sp.RemoveEstHoursForSprint(lbFeaturesInSprint.SelectedValue, hSumOfFeatHours.Value)
        lbFeaturesInSprint.Items.Remove(lbFeaturesInSprint.SelectedItem)
        hSumOfFeatHours.Value = _sp.PostSumOfFeatureHours
        lSumOfFeatHours.Text = hSumOfFeatHours.Value.ToString
        lRemainDevHours.Text = hSumOfDevHours.Value - hSumOfFeatHours.Value
        If lbFeaturesInSprint.Items.Count() > 0 Then
            lbFeaturesInSprint.SelectedIndex = 0
        End If
        If lbFeatures.Items.Count() > 0 Then
            pSubtasks.Visible = "true"
            lbFeatures.SelectedIndex = 0
            _bl.FillSubtasksforFeature(gvSubtasks, lbFeatures.SelectedValue)
            _sp.FillFeatureHoursSum(lbFeatures.SelectedValue)
            _sp.FillFeatureSubtaskCount(lbFeatures.SelectedValue)
            lFeatEstHours.Text = _sp.FeatureHours
            lSubtaskCount.Text = _sp.FeatureTaskCount
        Else
            pSubtasks.Visible = "false"
            pTaskAddEdit.Visible = "false"
        End If

    End Sub

    Protected Sub lbDevTeam_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbDevTeam.SelectedIndexChanged
        Dim _sp As New sprint

        Dim _count As Integer
        _count = 0
        For Each _item As ListItem In lbDevTeam.Items
            If _item.Selected = "True" Then
                _count = _count + 1
            End If
        Next

        _sp.FillDevHoursTotal(_count, calStartDate.SelectedDate, calEndDate.SelectedDate)
        _sp.FillDevTeamBullet(blDevTeam, lbDevTeam)
        hSumOfDevHours.Value = _sp.SprintTotalDevHours
        lSumOfDevHours.Text = _sp.SprintTotalDevHours
        lRemainDevHours.Text = hSumOfDevHours.Value - hSumOfFeatHours.Value
        lSummaryDevHoursValue.Text = _sp.SprintTotalDevHours


    End Sub

    Protected Sub calEndDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calEndDate.SelectionChanged
        Dim _sp As New sprint

        Dim _count As Integer
        _count = 0
        For Each _item As ListItem In lbDevTeam.Items
            If _item.Selected = "True" Then
                _count = _count + 1
            End If
        Next

        _sp.FillDevHoursTotal(_count, calStartDate.SelectedDate, calEndDate.SelectedDate)
        _sp.FillDevTeamBullet(blDevTeam, lbDevTeam)
        hSumOfDevHours.Value = _sp.SprintTotalDevHours
        lSumOfDevHours.Text = _sp.SprintTotalDevHours
        lRemainDevHours.Text = hSumOfDevHours.Value - hSumOfFeatHours.Value
        lSummaryDevHoursValue.Text = _sp.SprintTotalDevHours
    End Sub

    Protected Sub bCreateSprint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCreateSprint.Click
        CreateSprint()
        linkEditHeader.Enabled = False
        linkSprintDetailEdit.Enabled = False
        bCreateSprint.Visible = "False"
        bNewSprint.Visible = "True"

    End Sub

    Private Sub CreateSprint()
        Dim _sp As New sprint

        _sp.AddSprint(calStartDate.SelectedDate, calEndDate.SelectedDate)
        _sp.AddDevTeamToSprint(lbDevTeam, _sp.SprintId)
        _sp.AddFeaturesToSprint(lbFeaturesInSprint, _sp.SprintId)

        lOutcome.Text = String.Format("Sprint {0} created!", _sp.SprintId)
    End Sub

    Protected Sub linkEditHeader_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkEditHeader.Click
        pSprintHeaderSummary.Visible = "False"
        pSprintDetail.Visible = "False"
        pSprintHeader.Visible = "True"
        pSprintDetailSummary.Visible = "False"
        pSubtasks.Visible = "True"
    End Sub

    Protected Sub linkSprintDetailEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkSprintDetailEdit.Click
        bNewSprint.Enabled = "False"
        pSprintDetail.Visible = "True"
        pSprintDetailSummary.Visible = "False"
        lAddedFeatures.Visible = "False"
        pSubtasks.Visible = "True"
        pSprintDetailSummary.Visible = "False"

    End Sub

    Protected Sub gvSubtasks_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSubtasks.SelectedIndexChanged

		pTaskAddEdit.Visible = True
		lTaskOutcome.Visible = True
		lTaskOutcome.Text = ""
		FillPriorities(ddlPriorities)

		Dim _taskID As Integer = gvSubtasks.SelectedDataKey.Value

		hTaskId.Value = _taskID

		Try
			Dim _task As New task(_taskID)
			tTaskName.Text = _task.TaskName
			tDesc.Text = _task.TaskDesc
			ddlPriorities.SelectedValue = _task.PriorityID
			tEstTime.Text = _task.EstimatedTime
			_task = Nothing
		Catch ex As Exception
			lTaskOutcome.Text = ex.Message
		End Try

	End Sub

	Protected Sub bUpdateTask_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bUpdateTask.Click

		lTaskOutcome.Visible = True
		lTaskOutcome.Text = ""
		Dim _task As task

		Try
			_task = New task(hTaskId.Value)

			_task.TaskName = tTaskName.Text
			_task.TaskDesc = tDesc.Text
			_task.PriorityID = ddlPriorities.SelectedValue
			_task.EstimatedTime = tEstTime.Text

			_task.UpdateTask()
			lTaskOutcome.Text = "Subtask updated!"

			Dim _bl As New scrumrBL
			_bl.FillSubtasksforFeature(gvSubtasks, lbFeatures.SelectedValue)
			_bl = Nothing
		Catch ex As Exception
			lTaskOutcome.Text = ex.Message
		End Try

		_task = Nothing
	End Sub

End Class
